package com.briup.mt.web.controller;

import com.briup.mt.bean.Application;
import com.briup.mt.bean.Module;
import com.briup.mt.service.IModuleService;
import com.briup.mt.utils.ExcelUtils;
import com.briup.mt.utils.Message;
import com.briup.mt.utils.MessageUtil;
import com.sun.org.apache.bcel.internal.generic.MONITORENTER;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Validated
@RequestMapping("/module")
public class ModuleController {

    @Autowired
    private IModuleService moduleService;

    //查找
    @GetMapping("query")
    public Message query(String name, String algorithm, String introduce){
        List<Module> list =moduleService.query(name, algorithm, introduce);
        return MessageUtil.success(list);
    }

    //保存或更新
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Module module){
        moduleService.saveOrUpdate(module);
        return MessageUtil.success("操作成功");
    }

    //删除
    @DeleteMapping("deleteById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户编号", required = true, paramType = "query"),
    })
    public Message deleteById( @NotNull Long id){
        moduleService.deleteById(id);
        return MessageUtil.success("删除成功");
    }


    @ApiOperation(value = "导入到Excel中", notes = "注意！测试的时候请将地址粘贴到浏览器地址栏测试", produces = "module/octet-stream")
    @GetMapping("download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "应用名称", paramType = "query"),
    })
    public void download(HttpServletResponse response, String name, String algorithm, String introduce) throws Exception {
        // 1. 查询数据
        List<Module> list = moduleService.query(name,algorithm,introduce);
        // 2. 将数据填充excel导出
        // 2.1 excel头部数据
        String[] headList = new String[] { "名称", "算法","介绍","状态","创建时间","所属应用ID","用户ID", "数据集ID" };
        // 2.2 属性
        String[] fieldList = new String[] { "name", "algorithm","introduce","status","createTime","applicationId", "userId","datasetId"};
        // 2.3 数据列，需要转化
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (Module app : list) {
            // 每一个map相当于是一行数据，一行数据中多列
            Map<String, Object> map = new HashMap<>();
            map.put("name", app.getName());
            map.put("algorithm", app.getAlgorithm());
            map.put("introduce",app.getIntroduce());
            map.put("status",app.getStatus());
            map.put("createTime",app.getCreateTime());
            map.put("applicationId",app.getApplicationId());
            map.put("userId",app.getUserId());
            map.put("datasetId",app.getDatasetId());
            // dataList表示所有行
            dataList.add(map);
        }
        // 2.4 导出文件名
        String excelName = "module";
        // 3. 调用工具方法导出Excel
        ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);

    }



}
